﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace LiveMail
{
    /// <summary>
    /// smtp send mail.
    /// </summary>
    public class SmtpSendMail
    {
        private string userName;
        private SmtpClient smtp = new SmtpClient();
        /// <summary>
        /// constructor of class.
        /// </summary>
        /// <param name="smtpServer">server address.</param>
        /// <param name="port">server port.</param>
        /// <param name="user">user name.</param>
        /// <param name="pass">the password.</param>
        public SmtpSendMail(string smtpServer, int port, string user, string pass)
        {
            userName = user;

            smtp.Host = smtpServer;
            smtp.Port = port;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(user, pass);
        }

        /// <summary>
        /// use default microsoft credential to send mail. will not use ssl. 
        /// </summary>
        internal SmtpSendMail()
        {
            userName = Environment.UserName + "@microsoft.com";

            smtp.Host = "smtphost.redmond.corp.microsoft.com";
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = true;
        }

        /// <summary>
        /// send mail to users.
        /// </summary>
        /// <param name="subject">mail subject.</param>
        /// <param name="content">mail body.</param>
        /// <param name="toList">to list.</param>
        public void Send(string subject, string content, params string[] toList)
        {
            using (MailMessage mail = new MailMessage())
            {
                mail.From = new MailAddress(userName);
                mail.Subject = subject;
                mail.Body = content;
                mail.IsBodyHtml = true;
                foreach (string to in toList)
                {
                    mail.To.Add(new MailAddress(to));

                }
                smtp.Send(mail);
            }
        }
    }
}
